<script setup>
import { router } from '@/router'
import { useCounterStore } from '@/stores/login'
import { useCartStore } from '@/stores/cart'
const useUserstore = useCounterStore()
const CartStore = useCartStore()
// 退出时清除用户信息
const clearUserInfo = () => {
  // 清空用户信息
  useUserstore.clearUserInfo()
  // 同时清空购物车数据
  CartStore.clearCart()
  router.push('/login')
}
</script>

<template>
  <nav class="app-topnav">
    <div class="container">
      <ul>
        <template v-if="useCounterStore().userinfo.token">
          <li>
            <a href="javascript:;"
              ><i class="iconfont icon-user"></i
              >{{ useCounterStore().userinfo?.account }}</a
            >
          </li>
          <li>
            <el-popconfirm
              title="确认退出吗?"
              confirm-button-text="确认"
              cancel-button-text="取消"
              @confirm="clearUserInfo"
            >
              <template #reference>
                <a href="javascript:;">退出登录</a>
              </template>
            </el-popconfirm>
            <!-- <a href="javascript:;" @click="router.push('/login')" v-if="!useCounterStore().userinfo.token">请先登录</a> -->
          </li>
          <li>
            <a href="javascript:;" @click="router.push('/member/order')"
              >我的订单</a
            >
          </li>
          <li>
            <a href="javascript:;" @click="router.push('/member')">会员中心</a>
          </li>
          <li><a href="javascript:;">帮助中心</a></li>
          <li><a href="javascript:;">关于我们</a></li>
          <li><a href="javascript:;">手机版</a></li>
        </template>
        <template v-else>
          <li>
            <a href="javascript:;" @click="router.push('/login')">请先登录</a>
          </li>
          <li><a href="javascript:;">帮助中心</a></li>
          <li><a href="javascript:;">关于我们</a></li>
        </template>
      </ul>
    </div>
  </nav>
</template>

<style scoped lang="scss">
.app-topnav {
  background: #333;
  ul {
    display: flex;
    height: 53px;
    justify-content: flex-end;
    align-items: center;
    li {
      a {
        padding: 0 15px;
        color: #cdcdcd;
        line-height: 1;
        display: inline-block;

        i {
          font-size: 14px;
          margin-right: 2px;
        }

        &:hover {
          color: $xtxColor;
        }
      }

      ~ li {
        a {
          border-left: 2px solid #666;
        }
      }
    }
  }
}
</style>
